/* $Id: MessageS2CConnectNACK.java,v 1.5 2010/07/25 16:19:12 nhnb Exp $ */
/***************************************************************************
 *                      (C) Copyright 2003 - Marauroa                      *
 ***************************************************************************
 ***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/
package marauroa.common.net.message;

import java.io.IOException;
import java.nio.channels.SocketChannel;
/**
 * This message indicate the client that the server has rejected its connection
 * It will indicate that this client IP has been banned from server because:
 * a) Bad behaviour
 * b) Flooding server.
 * c) Any other reason at server's admin discrection
 *
 * @see marauroa.common.net.message.Message
 */
public class MessageS2CMigrate extends Message {

    public String name;
    public String ipToConnect;

    /** Constructor for allowing creation of an empty message */
    public MessageS2CMigrate() {
        super(MessageType.S2C_MIGRATE, null);
    }

    /**
     * Constructor with a TCP/IP source/destination of the message
     *
     * @param source
     *            The TCP/IP address associated to this message
     */
    public MessageS2CMigrate(SocketChannel source) {
        super(MessageType.S2C_MIGRATE, source);
    }

    public String getClientName() {
        return name;
    }

    public void setClientName(String name) {
        this.name = new String(name);
    }

    public String getIpToConnect() {
        return ipToConnect;
    }

    public void setIpToConnect(String ipToConnect) {
        this.ipToConnect = ipToConnect;
    }



    /**
     * This method returns a String that represent the object
     *
     * @return a string representing the object.
     */
    @Override
    public String toString() {
        return "Message (S2C Migrate) from (" + getAddress() + ") CONTENTS: ()" + name;
    }

    @Override
    public void writeObject(marauroa.common.net.OutputSerializer out) throws IOException {
        super.writeObject(out);
        out.write(this.getClientName());
        out.write(this.getIpToConnect());
    }

    @Override
    public void readObject(marauroa.common.net.InputSerializer in) throws IOException {
        super.readObject(in);
        this.setClientName(in.readString());
        this.setIpToConnect(in.readString());
        if (type != MessageType.S2C_MIGRATE) {
            throw new IOException();
        }
    }
}
